#include <stdlib.h>
#include <stddef.h>
#include "DIALOG.h"
#include "\src\mzhxj.h"
#include "Target\2410lib.h"
#include "Target\option.h"
#include "Target\2410addr.h"
#include "Target\timer.h"
#include "mmu\mmu.h"
#include "\gui\widget\framewin.h"

/*******************************************************************
*
*                 main
*
********************************************************************
*/
#include "stdio.h"
#include "UART.h"

extern volatile int flag_INT_UART0;
extern volatile int flag_INT_UART0_ERR;
extern volatile int flag_INT_UART0_RXD;
extern volatile int flag_INT_UART0_TXD;

char str[100];

void Main(void)
{
    char ch;
    int i,j,k,m,n;
	i=j=k=m=n=~0;

    MMU_Init();
    ChangeClockDivider(1,1);          // 1:2:4
    ChangeMPllValue(0xa1,0x3,0x1);    // FCLK=202.8MHz     
    Port_Init();
    GUI_Init();

///////////////////////////////////////////////////////////////
    rINTMSK = ~0;

	UART_SelectPort(0);
//	UART0_SetDefault();
    UART_Init(19000);//19200

    ClearPending(~0);


    while(1)
    {
    	if(i!=flag_INT_UART0)
    	{
    		i=flag_INT_UART0;
    		sprintf(str,"INT      : %d",i);
    		GUI_DispStringAt(str,100,80);
    		sprintf(str,"FIFO Stat: %d",rUFSTAT0);
    		GUI_DispStringAt(str,100,100);
    	}
    	if(j!=flag_INT_UART0_ERR)
    	{
    	    j=flag_INT_UART0_ERR;
    	    sprintf(str,"UART0 err: %d",j);
    	    GUI_DispStringAt(str,100,140);
    	}
    	if(k!=flag_INT_UART0_RXD)
    	{
    		k=flag_INT_UART0_RXD;
    		sprintf(str,"RX called: %d th",k);
    		GUI_DispStringAt(str,100,160);
    	}
    	if(m!=flag_INT_UART0_TXD)
    	{
    		m=flag_INT_UART0_TXD;
    		sprintf(str,"TX called: %d th",m);
    		GUI_DispStringAt(str,100,180);
    	}
		if(UART_kbhit())
		{
    		sprintf(str,"RH:%-4d; RT:%-4d; TH:%-4d; TT:%-4d",UART0_GetRxBufHead(),
    			UART0_GetRxBufTail(),UART0_GetTxBufHead(),UART0_GetTxBufTail());
	    	GUI_DispStringAt(str,100,200);
	    	UART_putchar(UART_getch());
	    	
		    GUI_DispStringAt("                                        ",100,230);
         	UART_gets(str);
         	GUI_DispStringAt(str,100,230);
		    UART_puts("China is powerful.");
		}
    }

///////////////////////////////////////////////////////////////////

    while(1)
    {
    }
}

/******************************************************************/
